package com.yanmaohu.nikita.compiler;

import com.yanmaohu.nikita.NikitaException;
import com.yanmaohu.nikita.configuration.Configuration;
import com.yanmaohu.nikita.linker.Linker;
import com.yanmaohu.nikita.utility.TimeUtility;
import com.yanmaohu.nikita.utility.ValueUtility;

import java.io.File;
import java.util.List;

/**
 * @author Bright Lee
 */
public final class Compiler {

    private final Configuration configuration;
    private final Linker linker;

    public Compiler(Configuration configuration) {
        this.linker = new Linker(this.configuration = configuration);
    }

    public void compile() {
        System.out.println("Nikita " + ValueUtility.VERSION + " " + configuration.getProjectRootDirectoryAbsolutePath());
        long beginTimes = System.currentTimeMillis();
        List<File> sourceCodeFileList = configuration.getSourceCodeFileList();
        try {
            linker.link(sourceCodeFileList);
        } catch (NikitaException e) {
            if (configuration.isPrintStackTrace()) {
                throw e;
            } else {
                System.out.println(e.getMessage());
            }
        }
        System.out.println(sourceCodeFileList.size() + " fils were compiled within " + TimeUtility.useTime(beginTimes) + ".");
    }

}
